
$(function(){
	var nowDate = new Date();
	$("#beginDate").html(getMonthStartDate());
	$("#endDate").html(dateTimeFormat("yyyy-MM-dd",nowDate));

	//第一次加载
	createmainTable()

	//确定按钮
	$("button#selectorBtnOK, #confirmBtn").click(function(){
		mui.back();
		reloadTable();
	});
	//-----------------------------查询界面开始---------------------------------------------------
	//为开始日期和结束日期设置默认值
	// $("#selectorBeginDateShow").html(getMonthStartDate());
	// $("#selectorEndDateShow").html(dateTimeFormat("yyyy-MM-dd",nowDate));
	//日期选择器
	$(".myPicker").click(function(){
		var self = this;
		//创建普通的popPicker
		var popPicker = new mui.PopPicker();
		var popDataArray = new Array();
		//创建日期形式的Depicker
		var datePicker = new mui.DtPicker({
			type: "date",
			endDate: nowDate
		});
		switch ($(self).attr('id')){
			//开始日期选择器
			case 'selectorBeginDate':
				datePicker.setSelectedValue(getMonthStartDate());
				datePicker.show(function(items) {
					$("#selectorBeginDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//结束日期选择器
			case 'selectorEndDate':
				datePicker.show(function(items) {
					$("#selectorEndDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//期初
			case 'selectorPay':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '是'},{value: '0' ,text: '否'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorPayShow").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			//摊销属性参数
			case 'selectorQrySubCateTX':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '资产'},{value: '0' ,text: '存货'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#qrySubCateTXShow").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			//内外
			case 'selectorQrySubjectNW':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '内部主体'},{value: '0' ,text: '外部主体'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorQrySubjectNWShow").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			default:
				break;
		}
	});


	//清空按钮
	$("button#selectorBtnCancel").click(function(){
		$('#qrySubjectS').val('');
		$('#qryWareS').val('');
		$('#selectorBeginDateShow').html('');
		$('#selectorEndDateShow').html('');
		$('#qryResS').val('');
		$('#selectorPayShow').attr('data-value', 0).html('否');
		$('#selectorQrySubjectNWShow').attr('data-value', 1).html('内部主体');
		$('#qrySubCateTXShow').attr('data-value', 0).html('存货');
	});


//-----------------------------查询界面结束---------------------------------------------------
});
//-----------------------------主界面开始---------------------------------------------------

var tableIns = '';
function createmainTable(){
	var limit = 20;
	var params = {
		qrySubject: $('#qrySubjectS').val(),
		qryWare: $('#qryWareS').val(),
		beginDate: $('#selectorBeginDateShow').html(),
		endDate: $('#selectorEndDateShow').html(),
		qryRes: $('#qryResS').val(),
		qryState: $('#qryStateS').val(),
		qryResQC: $('#selectorPayShow').attr('data-value'),
		qrySubjectNW: $('#selectorQrySubjectNWShow').attr('data-value'),
		qrySubCateTX: $('#qrySubCateTXShow').attr('data-value')
	};
	// console.log(params)
	$("#qryRes").html(params.qryRes|| '');
	$("#qrySubject").html(params.qrySubject|| '');
	$("#qryWare").html(params.qryWare|| '');
	$("#qryState").html(params.qryState|| '');
	$("#qryResQC").html($('#selectorPayShow').html());
	$('#qrySubjectNW').html($('#selectorQrySubjectNWShow').html());
	$('#qrySubCateTX').html($('#qrySubCateTXShow').html());
	$("#beginDate").html(params.beginDate|| '');
	$("#endDate").html(params.endDate|| '');
	$('#table').html("");
	layui.use('table', function(){
		var table = layui.table;

		//第一个实例
		tableIns = table.render({
			elem: '#table'
			,limit: limit
			// ,skin: 'line'
			,even: true
			,size: 'sm'
			,height: $(window).height() - $('.head-search').height() - 120
			,url: url1 + Utils.Api.rptMgntKcGet //数据接口
			,where: {
				accessid: window.localStorage.accessid || '',
				field: 'subjectNick',
				direction: 'ASC',
				qryDateB: params.beginDate|| '',
				qryDateE: params.endDate|| '',
				qrySubject: params.qrySubject || '', //主体
				qryWare: params.qryWare || '', //单元
				qrySubjectNW: params.qrySubjectNW || 1, //内外
				qryRes: params.qryRes || '',  //资源
				qryState: params.qryState || '', //状态
				qryResQC: params.qryResQC || 0, //期初
				qrySubCateTX: params.qrySubCateTX //摊销属性参数
			}
			,request: {
				pageName: 'pageIndex' //页码的参数名称，默认：page
				,limitName: 'limit' //每页数据量的参数名，默认：limit
			}
			, response: {
				statusName: 'hasError', //数据状态的字段名称，默认：code
				statusCode: false, //成功的状态码，默认：0
				msgName: 'error', //状态信息的字段名称，默认：msg
				countName: 'results', //数据总数的字段名称，默认：count
				dataName: 'rows' //数据列表的字段名称，默认：data
			}
			, page: true //开启分页
			,cols: [[ //表头
				{field: 'subjectNick', title: '主体/单元',align: 'center', width: 120,templet: function (d) {
					return !d.summary? d.subjectNick + '/' + d.wareName: '<span class="font-bold">' + d.summary+'</span>'
				}},
				// {field: 'wareName', title: '单元名称',align: 'center', width: 100},
				{field: 'resName', title: '资源/状态',align: 'center', width: 120,templet: function(d){
					return !d.summary? d.resName + '/' + d.stateName: ''
				}},
				// {field: 'stateName', title: '资源状态',align: 'center', width: 100},
				{field: 'unitName', title: '单位',align: 'center', width: 40},
				{field: 'qtySign', title: '结存数量',align: 'center', width: 80},
				{field: 'price', title: '结存单价',align: 'right', width: 100,templet: function (d) {
					return  Utils.formatMoney(d.price)
				}},
				{field: 'amountSign', title: '结存金额', align: 'right', width: 100,  templet: function (d) {
					return !d.summary?  Utils.formatMoney(d.amountSign): '<span class="font-bold">' +Utils.formatMoney(d.amountSign)+'</span>'
				}},
				{field: '', title: '操作',templet: '#titleTpl', align: 'center', width: 100},
			]]
			,sums: true
			,done: function(res){
				if(res.summary){
					$('#total').html(res.summary.amountSign)
				}
			}
		});

	});

	// var pageNo = params.pageIndex || 0;
	// Utils.request(Utils.Api.rptMgntKcGet, {
	// 	start: 10*pageNo,
	// 	limit: 10,
	// 	pageIndex: pageNo,
	// 	field: 'subjectNick',
	// 	direction: 'ASC',
	// 	qryDateB: params.beginDate|| '',
	// 	qryDateE: params.endDate|| '',
	// 	qrySubject: params.qrySubject || '', //主体
	// 	qryWare: params.qryWare || '', //单元
	// 	qrySubjectNW: params.qrySubjectNW || 1, //内外
	// 	qryRes: params.qryRes || '',  //资源
	// 	qryState: params.qryState || '', //状态
	// 	qryResQC: params.qryResQC || 0 //期初
	// }, function(data){
	// 	$('#table').html(template('tableTem', data));
	// 	$('#total').html(data.results);
	// 	var pageHtml = $('#main');
	// 	pageHtml.find('button').removeAttr('disabled');
	// 	if(data.rows.length < 10){
	// 		pageHtml.find('.next').attr('disabled', 'disabled')
	// 	}
	// 	if(pageNo == 0){
	// 		pageHtml.find('.pre').attr('disabled', 'disabled')
	// 	}
	// })
}

function reloadTable(){
	var params = {
		qrySubject: $('#qrySubjectS').val(),
		qryWare: $('#qryWareS').val(),
		beginDate: $('#selectorBeginDateShow').html(),
		endDate: $('#selectorEndDateShow').html(),
		qryRes: $('#qryResS').val(),
		qryState: $('#qryStateS').val(),
		qryResQC: $('#selectorPayShow').attr('data-value'),
		qrySubjectNW: $('#selectorQrySubjectNWShow').attr('data-value'),
		qrySubCateTX: $('#qrySubCateTXShow').attr('data-value')
	};
	// console.log(params)
	$("#qryRes").html(params.qryRes|| '');
	$("#qrySubject").html(params.qrySubject|| '');
	$("#qryWare").html(params.qryWare|| '');
	$("#qryState").html(params.qryState|| '');
	$("#qryResQC").html($('#selectorPayShow').html());
	$('#qrySubjectNW').html($('#selectorQrySubjectNWShow').html());
	$('#qrySubCateTX').html($('#qrySubCateTXShow').html());
	$("#beginDate").html(params.beginDate|| '');
	$("#endDate").html(params.endDate|| '');
	$('#table').html("");

	tableIns.reload({
		where: { //设定异步数据接口的额外参数，任意设
			accessid: window.localStorage.accessid || '',
			field: 'subjectNick',
			direction: 'ASC',
			qryDateB: params.beginDate|| '',
			qryDateE: params.endDate|| '',
			qrySubject: params.qrySubject || '', //主体
			qryWare: params.qryWare || '', //单元
			qrySubjectNW: params.qrySubjectNW || 1, //内外
			qryRes: params.qryRes || '',  //资源
			qryState: params.qryState || '', //状态
			qryResQC: params.qryResQC || 0, //期初
			qrySubCateTX: params.qrySubCateTX //摊销属性参数
		}
		,page: {
			curr: 1 //重新从第 1 页开始
		}
		// ,height: $(window).height() - $('.head-search').height() - 120
		,url: url1 + Utils.Api.rptMgntKcGet //数据接口
	});
}


function Detail(subjectId,unitId,wareId,resId,stateId){
	//账户资金余额明细
	var detailParams = {
		subjectId: subjectId,
		unitId: unitId,
		wareId: wareId,
		resId: resId,
		stateId: stateId
	}
	mui.openWindow({
		url: 'stockDetail.html',
	})
	window.localStorage.stockParams = JSON.stringify(detailParams)
}
